package com.yy.qpgame.server.netty.websocket;

import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

import static io.netty.buffer.Unpooled.wrappedBuffer;

public class WebSocketServerInitializer extends ChannelInitializer<Channel> {
    private int protocolCode ;
    private int packageLength ;


    @Override
    protected void initChannel(Channel ch) throws Exception {

        ChannelPipeline pipeline = ch.pipeline();
          
        pipeline.addLast(new HttpServerCodec());


        pipeline.addLast(new ChunkedWriteHandler());

        pipeline.addLast(new HttpObjectAggregator(64*1024));

//
        pipeline.addLast(new HttpRequestHandler("/ws"));
//


        pipeline.addLast(new WebSocketServerProtocolHandler("/ws"));


//        // 协议包解码
//        pipeline.addLast(new MessageToMessageDecoder<WebSocketFrame>() {
//
//
//
//
//            @Override
//            protected void decode(ChannelHandlerContext ctx, WebSocketFrame frame, List<Object> objs) throws Exception {
//                ByteBuf buf = ((BinaryWebSocketFrame) frame).content();
//                packageLength = buf.readInt();
//                protocolCode = buf.readInt();
//                System.out.println("协议包解码packageLength = " + packageLength + " protocolCode = " + protocolCode);
//                int len = buf.readableBytes();
//                byte[] data = new byte[len];
//                ByteBuf bytes = buf.getBytes(buf.readerIndex(), data);
//                objs.add(bytes);
//                buf.retain();
//
//            }
//        });

////         协议包编码
//        pipeline.addLast(new MessageToMessageEncoder<MessageLiteOrBuilder>() {
//            @Override
//            protected void encode(ChannelHandlerContext ctx, MessageLiteOrBuilder msg, List<Object> out) throws Exception {
//                ByteBuf result = null;
//
//
//                if (msg instanceof MessageLite) {
//                    result = wrappedBuffer(((MessageLite) msg).toByteArray());
//                }
//                if (msg instanceof MessageLite.Builder) {
//                    result = wrappedBuffer(((MessageLite.Builder) msg).build().toByteArray());
//                }
//
//                // ==== 上面代码片段是拷贝自TCP ProtobufEncoder 源码 ====
//                // 然后下面再转成websocket二进制流，因为客户端不能直接解析protobuf编码生成的
//
//
//                WebSocketFrame frame = new BinaryWebSocketFrame(result);
//                out.add(frame);
//
//            }
//        });

        //protobuf
//        pipeline.addLast(new ProtobufVarint32FrameDecoder());
//        pipeline.addLast(new ProtobufDecoder(loginApiInfo.LoginRequest.getDefaultInstance()));
//        pipeline.addLast(new ProtobufVarint32LengthFieldPrepender());
//        pipeline.addLast(new ProtobufEncoder());
        //protobuf


        pipeline.addLast(new TextWebSocketFrameHandler());







    }
}